package com.clementheliou.cheliou.web.controller.gallery;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import javax.faces.component.UIComponent;

import org.primefaces.model.LazyDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;

import com.clementheliou.cheliou.dal.model.base.enumeration.SortOrder;
import com.clementheliou.cheliou.dal.model.photo.enumeration.PhotoSortableColumn;
import com.clementheliou.cheliou.web.controller.gallery.photo.model.PhotoLazyDataModel;

/**
 * Controller managing the gallery views. It's scoped to an HTTP session lifecycle.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link Controller};
 * @see {@link InitializingBean};
 * @see {@link Scope};
 * @see {@link Serializable}.
 * @since 1.0
 */
@Controller
@Scope("session")
public class GalleryBean implements InitializingBean, Serializable {

	/**
	 * SLF4J logger for this class.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(GalleryBean.class);

	/**
	 * UID for serialization.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private static final long serialVersionUID = 3178696435625893478L;

	/**
	 * Contains the columns that can be used to sort the grid results. It will be load only once
	 * because the sortable columns won't be change during the HTTP session lifecycle.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private List<PhotoSortableColumn> sortableColumns;

	/**
	 * Contains the orders that can be used to sort the grid results. It will be load only once
	 * because the sort orders won't be change during the HTTP session lifecycle.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private List<SortOrder> sortOrders;

	/**
	 * {@link LazyDataModel} binded to the photos data grid.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link LazyDataModel}.
	 * @since 1.0
	 */
	@Autowired
	private transient PhotoLazyDataModel photosModel;

	/**
	 * <i>{@inheritDoc}</i>
	 * <p>
	 * Checks that all required dependencies are set.
	 * </p>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(photosModel, "photosModel must not be null at this time");
	}

	/**
	 * Gets the sortable columns that can be used into the {@link UIComponent} as a {@link List} .
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the sortable columns.
	 * @since 1.0
	 */
	public List<PhotoSortableColumn> getSortableColumns() {
		if (null == sortableColumns) {
			LOGGER.debug("Loading the sortable columns.");
			sortableColumns = Arrays.asList(PhotoSortableColumn.values());
		}

		return sortableColumns;
	}

	/**
	 * Gets the sort orders that can be used into the {@link UIComponent} as a {@link List}.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the sort orders.
	 * @since 1.0
	 */
	public List<SortOrder> getSortOrders() {
		if (null == sortOrders) {
			LOGGER.debug("Loading the sort orders.");
			sortOrders = Arrays.asList(SortOrder.values());
		}

		return sortOrders;
	}

	/*
	 * Getters & setters.
	 */

	public PhotoLazyDataModel getPhotosModel() {
		return photosModel;
	}

}
